package com.byb.server.service.impl;

import com.byb.server.service.CrowdService;
import com.byb.server.domain.Crowd;
import com.byb.server.repository.CrowdRepository;
import com.byb.server.repository.search.CrowdSearchRepository;
import com.byb.server.service.dto.CrowdDTO;
import com.byb.server.service.mapper.CrowdMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Crowd.
 */
@Service
@Transactional
public class CrowdServiceImpl implements CrowdService{

    private final Logger log = LoggerFactory.getLogger(CrowdServiceImpl.class);

    private final CrowdRepository crowdRepository;

    private final CrowdMapper crowdMapper;

    private final CrowdSearchRepository crowdSearchRepository;
    public CrowdServiceImpl(CrowdRepository crowdRepository, CrowdMapper crowdMapper, CrowdSearchRepository crowdSearchRepository) {
        this.crowdRepository = crowdRepository;
        this.crowdMapper = crowdMapper;
        this.crowdSearchRepository = crowdSearchRepository;
    }

    /**
     * Save a crowd.
     *
     * @param crowdDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public CrowdDTO save(CrowdDTO crowdDTO) {
        log.debug("Request to save Crowd : {}", crowdDTO);
        Crowd crowd = crowdMapper.toEntity(crowdDTO);
        crowd = crowdRepository.save(crowd);
        CrowdDTO result = crowdMapper.toDto(crowd);
        //crowdSearchRepository.save(crowd);
        return result;
    }

    /**
     *  Get all the crowds.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CrowdDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Crowds");
        return crowdRepository.findAll(pageable)
            .map(crowdMapper::toDto);
    }

    /**
     *  Get one crowd by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public CrowdDTO findOne(Long id) {
        log.debug("Request to get Crowd : {}", id);
        Crowd crowd = crowdRepository.findOne(id);
        return crowdMapper.toDto(crowd);
    }

    /**
     *  Delete the  crowd by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Crowd : {}", id);
        crowdRepository.delete(id);
        //crowdSearchRepository.delete(id);
    }

    /**
     * Search for the crowd corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CrowdDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Crowds for query {}", query);
        Page<Crowd> result = crowdSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(crowdMapper::toDto);
    }

    @Override
    public CrowdDTO getByProductId(Long id) {
        log.debug("Request to get Crowd By ProductId : {}", id);
        return  crowdMapper.toDto(crowdRepository.findByProductId(id));
    }
}
